﻿using UnityEngine;
using System.Collections;

public class mUIWrapContent : UIWrapContent
{

    public mEndLessCoverFlow endLessCoverFlow;

    void OnEnable()
    {
        endLessCoverFlow = GetComponent<mEndLessCoverFlow>(); 
    }

    protected override void Start()
    {
        SortBasedOnScrollMovement();
        
        WrapContent();

        if (mScroll != null)
        {
            mScroll.GetComponent<UIPanel>().onClipMove += OnMove;
            mScroll.restrictWithinPanel = false;
            
            if (mScroll.dragEffect == UIScrollView.DragEffect.MomentumAndSpring)
                mScroll.dragEffect = UIScrollView.DragEffect.Momentum;
        }
    }

    public override void SortBasedOnScrollMovement()
    {
        if (!CacheScrollView()) return;

        // Cache all children and place them in order
        mChildren.Clear();
        for (int i = 0; i < mTrans.childCount; ++i)
            mChildren.Add(mTrans.GetChild(i));

        // Sort the list of children so that they are in order
        if (mHorizontal) mChildren.Sort(UIGrid.SortHorizontal);
        else mChildren.Sort(UIGrid.SortVertical);
    }

    protected override void UpdateItem(Transform item, int index)
    {
        //if (endLessCoverFlow.mChildren.Peek() == item)
        //{
        //    //dequeue itself
        //    endLessCoverFlow.mChildren.Dequeue();

        //    //enqueue itself
        //    endLessCoverFlow.mChildren.Enqueue(item.GetComponent<mEndLessEntry>());        
        //}
        //else
        //{
        //    Debug.Log(" this ? " + endLessCoverFlow.mChildren.Peek().name 
        //            + " and " + item.name);
        //}
    }
}
